package com.food.service.impl;

import com.food.service.FileService;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class FileServiceImpl implements FileService {
    private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);
    
    @Value("${app.base-url}")
    private String baseUrl;
    
    @Override
    public String uploadFile(MultipartFile file) throws IOException {
        // 确保文件是图片
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            throw new IllegalArgumentException("只支持上传图片文件");
        }
        
        // 获取文件扩展名
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename != null ? 
            originalFilename.substring(originalFilename.lastIndexOf(".")) : ".jpg";
            
        // 生成唯一文件名
        String fileName = UUID.randomUUID().toString() + extension;
        
        // 构建保存路径 - 保存到 static/images/avatar 目录
        Path uploadPath = Paths.get("src/main/resources/static/images/avatar").toAbsolutePath();
        
        // 确保目录存在
        Files.createDirectories(uploadPath);
        
        // 保存文件
        Path filePath = uploadPath.resolve(fileName);
        Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
        
        logger.info("File saved to: {}", filePath);
        
        // 返回可访问的URL
        return baseUrl + "/static/images/avatar/" + fileName;
    }

    @Override
    public String uploadFoodImage(MultipartFile file) throws IOException {
        // 确保文件是图片
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            throw new IllegalArgumentException("只支持上传图片文件");
        }
        
        // 获取文件扩展名
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename != null ? 
            originalFilename.substring(originalFilename.lastIndexOf(".")) : ".jpg";
            
        // 生成唯一文件名
        String fileName = UUID.randomUUID().toString() + extension;
        
        // 构建保存路径 - 保存到 static/images/foods 目录
        Path uploadPath = Paths.get("src/main/resources/static/images/foods").toAbsolutePath();
        
        // 确保目录存在
        Files.createDirectories(uploadPath);
        
        // 保存文件
        Path filePath = uploadPath.resolve(fileName);
        Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
        
        logger.info("Food image saved to: {}", filePath);
        
        // 返回相对路径
        return "/static/images/foods/" + fileName;
    }

    @Override
    public String uploadRecipeImage(MultipartFile file) throws IOException {
        // 确保文件是图片
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            throw new IllegalArgumentException("只支持上传图片文件");
        }
        
        // 获取文件扩展名
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename != null ? 
            originalFilename.substring(originalFilename.lastIndexOf(".")) : ".jpg";
            
        // 生成唯一文件名
        String fileName = UUID.randomUUID().toString() + extension;
        
        // 构建保存路径 - 保存到 static/images/recipes 目录
        Path uploadPath = Paths.get("src/main/resources/static/images/recipes").toAbsolutePath();
        
        // 确保目录存在
        Files.createDirectories(uploadPath);
        
        // 保存文件
        Path filePath = uploadPath.resolve(fileName);
        Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
        
        logger.info("Recipe image saved to: {}", filePath);
        
        // 返回相对路径
        return "/static/images/recipes/" + fileName;
    }
} 